package com.roomwrod.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.roomwrod.R;
import com.roomwrod.livedata.viewmodel.WordViewModel;
import com.roomwrod.model.Word;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

public class WordAdapter extends ListAdapter<Word, WordAdapter.ViewHolder> {

    WordViewModel viewModel;

    public WordAdapter(WordViewModel viewModel) {
        super(new DiffUtil.ItemCallback<Word>() {
            @Override
            public boolean areItemsTheSame(@NonNull Word oldItem, @NonNull Word newItem) {
                return oldItem.getId() == newItem.getId();
            }

            @Override
            public boolean areContentsTheSame(@NonNull Word oldItem, @NonNull Word newItem) {
                return oldItem.getWords().equals(newItem.getWords()) && oldItem.getChinese().equals(newItem.getChinese());
            }
        });
        this.viewModel = viewModel;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_word, parent, false);
        final ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                Word word = (Word) viewHolder.itemView.getTag(R.id.word_item_ids);
                if (isChecked) {
                    viewHolder.tv_chinese.setVisibility(View.GONE);
                    word.setIsshowChines(true);
                } else {
                    viewHolder.tv_chinese.setVisibility(View.VISIBLE);
                    word.setIsshowChines(false);
                }
                viewModel.update(word);
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Word word = getItem(position);
        holder.itemView.setTag(R.id.word_item_ids, word);
        holder.tv_id.setText("" + word.getId());
        holder.tv_chinese.setText(word.getChinese());
        holder.tv_english.setText(word.getWords());
        if (word.isIsshowChines()) {
            holder.tv_chinese.setVisibility(View.GONE);
            holder.sw.setChecked(true);
        } else {
            holder.tv_chinese.setVisibility(View.VISIBLE);
            holder.sw.setChecked(false);
        }


    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_id, tv_english, tv_chinese;
        Switch sw;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_id = itemView.findViewById(R.id.tv_id);
            tv_english = itemView.findViewById(R.id.tv_english);
            tv_chinese = itemView.findViewById(R.id.tv_chinese);
            sw = itemView.findViewById(R.id.sw);
        }
    }
}
